// dianti/dianti_xq/dianti_xq.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    title: '维修',
    heights: '',
    elelist: [{
        id: 1,
        name: '自动扶梯',
      },
      {
        id: 2,
        name: '自动人行道',
      },
      {
        id: 3,
        name: '乘客电梯',
      },
      {
        id: 4,
        name: '载货电梯',
      }
    ], //  电梯
    elevator: '请选择维保单位', //电梯
    weib_type: [{
      name: '普通'
    }, {
      name: '保养'
    }, {
      name: '维修'
    }],
    weilei_guo: '请选择维保类型',
    photo: [],
    wuphoto: '',
    photohou: [],
    wuphotohou: '',
    wei_qian: yuming + 'upload/images/2022-09-02/631158b6935ad.png',
    is_wei: '', //0维修 1保养
    wenzi:'',
  },
  // 设备类型
  elevator(e) {
    let index = e.detail.value
    this.setData({
      elevator: this.data.elelist[index].name,
      eleid: this.data.elelist[index].id,
    });
  },
  // 维保类型
  bindwei_type(e) {
    let index = e.detail.value
    this.setData({
      weilei_guo: this.data.weib_type[index].name
    });
  },
  bininput(e) {
    let val = e.detail.value
    let modal = e.currentTarget.dataset.modal
    this.setData({
      [modal]: val
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var state = e.currentTarget.dataset.state
    console.log(state)
    var photohou = this.data.photohou;
    var wuphotohou = this.data.wuphotohou;
    if (state == 0) {
      var dd = wuphoto.split(",")
      console.log(dd)
      photo.splice(index, 1)
      dd.splice(index, 1)
      var rr = dd.join(",")
      this.setData({
        photo: photo,
        wuphoto: rr
      })
    }
    if (state == 1) {
      var dd = wuphotohou.split(",")
      console.log(dd)
      photohou.splice(index, 1)
      dd.splice(index, 1)
      var rr = dd.join(",")
      this.setData({
        photohou: photohou,
        wuphotohou: rr
      })
    }
  },
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    var wuphotohou = this.data.wuphotohou;
    console.log(e)
    var state = e.currentTarget.dataset.state
    this.setData({
      state: state
    })
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.wuduoupimg(tempFilePaths[i]).then(res => {
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                if (state == 0) {
                  wuphoto = wuphoto.concat(data.imgUrl + ",")
                  that.setData({
                    photo: that.data.photo.concat(urls),
                    wuphoto: wuphoto
                  });
                }
                if (state == 1) {
                  wuphotohou = wuphotohou.concat(data.imgUrl + ",")
                  that.setData({
                    photohou: that.data.photohou.concat(urls),
                    wuphotohou: wuphotohou
                  });
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  fangda() {
    wx.previewImage({
      urls: [this.data.dibu_img]
    })
  },
  // 返回
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      tab_gao: wx.getMenuButtonBoundingClientRect().top + 40,
      title: options.title,
      is_wei: options.is_wei
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (this.data.is_wei == 0) {
      this.setData({
        wenzi: '维修'
      })
    }
    if (this.data.is_wei == 1) {
      this.setData({
        wenzi: '保养'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})